package common.util;

import static common.constants.CommonConstants.ALIVE_KEY;
import static common.constants.CommonConstants.APPLICATION_KEY;
import static common.constants.CommonConstants.CORE_THREADS_KEY;
import static common.constants.CommonConstants.DEFAULT_KEY_PREFIX;
import static common.constants.CommonConstants.GROUP_KEY;
import static common.constants.CommonConstants.INVOKER_LISTENER_KEY;
import static common.constants.CommonConstants.MAGIC_VERSION_KEY;
import static common.constants.CommonConstants.METHODS_KEY;
import static common.constants.CommonConstants.QUEUES_KEY;
import static common.constants.CommonConstants.REFERENCE_FILTER_KEY;
import static common.constants.CommonConstants.RELEASE_KEY;
import static common.constants.CommonConstants.REMOTE_APPLICATION_KEY;
import static common.constants.CommonConstants.THREADPOOL_KEY;
import static common.constants.CommonConstants.THREADS_KEY;
import static common.constants.CommonConstants.THREAD_NAME_KEY;
import static common.constants.CommonConstants.TIMESTAMP_KEY;
import static common.constants.CommonConstants.TRANSPORTER_KEY;
import static common.constants.CommonConstants.VERSION_KEY;
import static common.constants.RpcConstants.TAG_KEY;

import common.model.MagicUrl;
import java.util.HashMap;
import java.util.Map;
/**
 * ClusterUtils
 */
public class ClusterUtils {

    private ClusterUtils() {
    }

    public static MagicUrl mergeUrl(MagicUrl remoteUrl, Map<String, String> localMap) {
        Map<String, String> map = new HashMap<String, String>();
        Map<String, String> remoteMap = remoteUrl.getParameters();

        if (remoteMap != null && remoteMap.size() > 0) {
            map.putAll(remoteMap);

            // Remove configurations from provider, some items should be affected by provider.
            map.remove(THREAD_NAME_KEY);
            map.remove(DEFAULT_KEY_PREFIX + THREAD_NAME_KEY);

            map.remove(THREADPOOL_KEY);
            map.remove(DEFAULT_KEY_PREFIX + THREADPOOL_KEY);

            map.remove(CORE_THREADS_KEY);
            map.remove(DEFAULT_KEY_PREFIX + CORE_THREADS_KEY);

            map.remove(THREADS_KEY);
            map.remove(DEFAULT_KEY_PREFIX + THREADS_KEY);

            map.remove(QUEUES_KEY);
            map.remove(DEFAULT_KEY_PREFIX + QUEUES_KEY);

            map.remove(ALIVE_KEY);
            map.remove(DEFAULT_KEY_PREFIX + ALIVE_KEY);

            map.remove(TRANSPORTER_KEY);
            map.remove(DEFAULT_KEY_PREFIX + TRANSPORTER_KEY);
        }

        if (localMap != null && localMap.size() > 0) {
            Map<String, String> copyOfLocalMap = new HashMap<>(localMap);
            copyOfLocalMap.remove(GROUP_KEY);
            copyOfLocalMap.remove(RELEASE_KEY);
            copyOfLocalMap.remove(MAGIC_VERSION_KEY);
            copyOfLocalMap.remove(VERSION_KEY);
            copyOfLocalMap.remove(METHODS_KEY);
            copyOfLocalMap.remove(TIMESTAMP_KEY);
            copyOfLocalMap.remove(TAG_KEY);

            map.putAll(copyOfLocalMap);

            map.put(REMOTE_APPLICATION_KEY, remoteMap.get(APPLICATION_KEY));

            // Combine filters and listeners on Provider and Consumer
            String remoteFilter = remoteMap.get(REFERENCE_FILTER_KEY);
            String localFilter = copyOfLocalMap.get(REFERENCE_FILTER_KEY);
            if (remoteFilter != null && remoteFilter.length() > 0
                    && localFilter != null && localFilter.length() > 0) {
                map.put(REFERENCE_FILTER_KEY, remoteFilter + "," + localFilter);
            }
            String remoteListener = remoteMap.get(INVOKER_LISTENER_KEY);
            String localListener = copyOfLocalMap.get(INVOKER_LISTENER_KEY);
            if (remoteListener != null && remoteListener.length() > 0
                    && localListener != null && localListener.length() > 0) {
                map.put(INVOKER_LISTENER_KEY, remoteListener + "," + localListener);
            }
        }

        return remoteUrl.clearParameters().addParameters(map);
    }

}
